﻿#include "ControlRobotMoveWidget.h"
#include "qmimedata.h"
#include "ui_ControlRobotMoveWidget.h"
#include "UiClass.h"
#include <QSettings>
#include "CustomControls/Buttons/PressPushButton.h"
#include "ControlComman.h"
#include "SteerMeter.h"
#include "SettingRobotSpeed.h"
#include "ControlTimer.h"
#include "SteerAngleDlg.h"
#include "PeripheralListDlg.h"
#include "CustomControls/ShowMessage/CMaskWidget.h"
#include "PublicClass/SendGlobalMsg.h"

namespace RO_Robot{
ControlRobotMoveWidget::ControlRobotMoveWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ControlRobotMoveWidget)
{
    ui->setupUi(this);
    ui->frame->setObjectName("frame_transparent");
    //这个会把全局的事件都勾过来.
    //    this->grabKeyboard();//必须添加，K 大写
    //清除模型文件中已激活的外设设备.
    PublicClass::init()->_deviceActivatedList.clear();
    //

    // _peripheralBtn = new QPushButton(("P"));
    // _peripheralBtn->setToolTip(tr("Peripheral Control"));
    // _peripheralBtn->setFixedSize(24,24);

    // connect(_peripheralBtn,SIGNAL(clicked(bool)),this,SLOT(slotPeripheralBtnCliecked(bool)));
    // //ui->gridLayout_button->addWidget(_peripheralBtn,0,0);

    // _peripheralBtn->setObjectName("btnZero");
    // //非机器人隐藏这些按钮
    // _peripheralBtn->setVisible(RobotInterface::E_Cell_Robot == PublicClass::init()->_iCellType? true : false);
    _cMaskWidget = new CMaskWidget(UiClass::init()->getMainWindow());
    _cMaskWidget->setBackgroundColor(QColor(0,0,0,0));
    _cMaskWidget->setVisible(false);

    connect(SendGlobalMsg::init(),&SendGlobalMsg::sigSendGlobalMsg,this,&ControlRobotMoveWidget::slotSendGlobalMsg);

}

/**释放内存
 * @brief ControlRobotMoveWidget::~ControlRobotMoveWidget
 */
ControlRobotMoveWidget::~ControlRobotMoveWidget()
{
   // if(_peripheralBtn) delete _peripheralBtn;
    if(_cMaskWidget){
        delete  _cMaskWidget;
    }
    while(!_shortcutList.isEmpty()){
        delete _shortcutList.takeFirst();
    }
    //FIX 之所以不用 deleteLater 而使用delete,
    //是因为delete 可以立即释放,而 deleteLater 需要等待下一次循环再释放
    //,虽然它有循环析构子QObject类,但我还是不能用它
    if(pSteerMeter){
        //        pSteerMeter->deleteLater();
        delete pSteerMeter;
    }
    if(pControlTimer){
        //        pControlTimer->deleteLater();
        delete pControlTimer;
    }
    if(pSettingRobotSpeed){
        //        pSettingRobotSpeed->deleteLater();
        delete pSettingRobotSpeed;
    }
    if(_periperalListDlg){
        //        _periperalListDlg->deleteLater();
        delete _periperalListDlg;
    }
    if(_steerAngleDlg) delete _steerAngleDlg;
    delete ui;
}

/**初始化（已在RobotWidget::slotRoTcpManagerStatus中调用）
 * @brief ControlRobotMoveWidget::init
 */
void ControlRobotMoveWidget::init()
{
    auto tcpManager = RobotManager::init()->getCurrentRobot();
    _steerList.clear();
    if(tcpManager && tcpManager->isConnected()){
        _robokitVersion = tcpManager->deviceStateInfo()->version;
        if(!pControlTimer){
            pControlTimer = new ControlTimer(this);
            connect(pControlTimer,&ControlTimer::sigControlSpeed,this,&ControlRobotMoveWidget::slotControlSpeed);
            connect(pControlTimer,&ControlTimer::sigControlStateChanged,this,&ControlRobotMoveWidget::sigControlStateChanged);

            connect(pControlTimer,&ControlTimer::sigKeyChecked,ui->directionWidget,&DirectionalPad::slotKeyChecked);
            connect(pControlTimer,&ControlTimer::sigKeyUnChecked,ui->directionWidget,&DirectionalPad::slotKeyUnChecked);
            //--解析model-----------初始化
            int mode = 0;//调试.
            ControlComman::init()->_isHasGroundCattle = false;
            ControlComman::init()->_pumpDO = -1;
            ControlComman::init()->_leakDO = -1;

            PublicClass::init()->_deviceActivatedList.clear();
            _motorList.clear();

            _mode = mode;
            //----------------------------------------------------------------------------------------
            QSettings read(PublicClass::init()->getTaskConfigIniFilePath(), QSettings::IniFormat);//当前目录的INI文件.
            QString key = "speedModel";
            if(4 == mode){
                key = "speedModel4";
            }

            QString maxRot = read.value(key +"/maxRot").toString();
            if(maxRot.isEmpty()){
                if(4 == mode){ //舵轮速度慢点
                    pControlTimer->setMaxRot(30);
                    pControlTimer->setMaxSpeed(0.2);
                }else{
                    pControlTimer->setMaxRot(45);
                    pControlTimer->setMaxSpeed(0.5);
                }
            }else{
                pControlTimer->setMaxRot(maxRot.toDouble());
                pControlTimer->setMaxSpeed(read.value(key+"/maxSpeed").toDouble());
            }

            initMode(mode);
            pControlTimer->setCompatibilityMode(isCompatibilityMode());
        }
        this->setValue(true);
        this->setEnabled(true);
    }else{
        //停止所有定时器
        stopAllTimer();
        if(pSettingRobotSpeed && pSettingRobotSpeed->isVisible()){
            pSettingRobotSpeed->close();
        }
        if(_periperalListDlg && _periperalListDlg->isVisible()){
            _periperalListDlg->close();
        }
        this->setValue(false);
        this->setEnabled(false);
    }

}

/**创建控制按钮
 * @brief ControlRobotMoveWidget::createMoveButton
 * @param toolTip
 * @param objectName
 * @param shortcutStr
 * @return
 */
// QPushButton *ControlRobotMoveWidget::createMoveButton(
//     const QString & toolTip,
//     const QString & objectName,
//     const QString & shortcutStr)
// {
//     QPushButton *btn = new QPushButton(this);
//     btn->setCheckable(true);
//     btn->setObjectName(objectName);
//     btn->setToolTip(toolTip);
//     //自动重复.
//     btn->setAutoRepeat(false);
//     //设置了自动重复的延迟时间为 100 毫秒,也就是说用户按下按钮后需要等待 100 毫秒才会开始自动重复.
//     // btn->setAutoRepeatDelay(100);
//     //设置了自动重复的间隔时间为 100 毫秒,也就是说在用户持续按下按钮时,每 100 毫秒会发送一次 clicked() 信号.
//     btn->setAutoRepeatInterval(100);
//     btn->setProperty(M_Key,shortcutStr);      //属性--值
//     if(pControlTimer){
//       pControlTimer->setKeyTimer(shortcutStr,btn);
//      }

//      return nullptr;
//  }

/**根据控制类型，初始化控制界面
 * @brief ControlRobotMoveWidget::initMode
 * @param mode
 */
void ControlRobotMoveWidget::initMode(int mode)
{
    _shortcutList.clear();
    if(pControlTimer)
        pControlTimer->setChassisMode(_mode);
    //    SCDebug<<"_model_chassis_mode:"<<_mode;
    //加入机器人控制快捷键
    _shortcutKeys.clear();

    QIcon setIcon;
    setIcon.addPixmap(QPixmap(QString("%1/images/RobotLibrary/set.svg").arg(StyleObject::init()->currentSkinDirPath())));
    ui->toolSet->setIcon(setIcon);
    ui->toolSet->setIconSize(QSize(26,26));
    connect(ui->toolSet,SIGNAL(clicked(bool)),this,SLOT(slotRobotControlSettingButton()));
   // ui->toolSet->setObjectName("pushButton_setting");
    ui->toolSet->setToolTip(tr("Setting parameters"));

    // if(RobotInterface::E_Cell_Robot == PublicClass::init()->_iCellType){
    //     ui->gamToolBtn->setStyleSheet("border:0px;background:none;background-image:none;");
    //     //            gameHandleBtn->setObjectName("btnZero");
    //     connect(ui->gamToolBtn,SIGNAL(clicked(bool)),this,SIGNAL(sigOpenJoyStickConfigDlg()));
    //     ui->gamToolBtn->setToolTip(tr("Configuration handle"));
    //     QIcon icon;
    //     icon.addPixmap(QPixmap(QString("%1/images/RobotLibrary/game_handle").arg(StyleObject::init()->currentSkinDirPath())));
    //     icon.addPixmap(QPixmap(QString("%1/images/RobotLibrary/game_handle").arg(StyleObject::init()->currentSkinDirPath())),
    //                    QIcon::Normal,QIcon::On);
    //     ui->gamToolBtn->setIcon(icon);
    //     ui->gamToolBtn->setIconSize(QSize(26,26));
    // }

    // QPushButton*btnW = createMoveButton(tr("Forward"),"pushButton_GoForward","W");
    // QPushButton*btnS = createMoveButton(tr("Backward"),"pushButton_GoBack","S");
    //QPushButton*btnA = createMoveButton(tr("Turnleft"),"pushButton_TurnLeft","A");
    //QPushButton*btnD = createMoveButton(tr("Turnright"),"pushButton_TurnRight","D");
    // QPushButton*btnQ = createMoveButton(tr("Emergency stop: Immediate braking"),"pushButton_GoStop","Q");
    // QPushButton*btnE = createMoveButton(tr("Shiftleft"),"pushButton_GoLeft","E");
    // QPushButton*btnR = createMoveButton(tr("Shiftright"),"pushButton_GoRight","R");

    QIcon icon;
    icon.addPixmap(QPixmap(QString("%1/images/RobotLibrary/triangleLeft.svg").arg(StyleObject::init()->currentSkinDirPath())));
    ui->btnE->setIcon(icon);
    ui->btnE->setIconSize(QSize(26,26));
    ui->btnE->setCheckable(true);
    ui->btnE->setProperty(M_Key,"E");      //属性--值
    if(pControlTimer){
        pControlTimer->setKeyTimer("E",ui->btnE);
    }

    icon.addPixmap(QPixmap(QString("%1/images/RobotLibrary/triangleRight.svg").arg(StyleObject::init()->currentSkinDirPath())));
    ui->btnR->setIcon(icon);
    ui->btnR->setIconSize(QSize(26,26));
    ui->btnR->setCheckable(true);
    ui->btnR->setProperty(M_Key,"R");
    if(pControlTimer){
        pControlTimer->setKeyTimer("R",ui->btnR);
    }

    if(pControlTimer){
        DirectionalPad* padPtr = ui->directionWidget;
        QPainterPath* pathPtr = &(ui->directionWidget->_buttons[1]); // Up
        pControlTimer->setKeyTimer(padPtr, pathPtr,"W");

        pathPtr = &(ui->directionWidget->_buttons[2]);//leftRotate
        pControlTimer->setKeyTimer(padPtr,pathPtr,"A");

        pathPtr = &(ui->directionWidget->_buttons[3]);//Down
        pControlTimer->setKeyTimer(padPtr,pathPtr,"S");

        pathPtr = &(ui->directionWidget->_buttons[4]);//rightRotate
        pControlTimer->setKeyTimer(padPtr,pathPtr,"D");

        pathPtr = &(ui->directionWidget->_centerButtons[0]); //Pause
        pControlTimer->setKeyTimer(padPtr,pathPtr,"Q");
    }

    ui->btnE->hide();
    ui->btnR->hide();
    switch (mode) {
    case 4://舵轮
    {
        _shortcutKeys<<"W"<<"S"<<"A"<<"D"<<"E"<<"Q";
        auto btnZero = new QPushButton(this);
        btnZero->setObjectName("btnZero");
        btnZero->setText(QStringLiteral("N°"));
        btnZero->setFixedSize(24,24);
        connect(btnZero,SIGNAL(clicked(bool)),this,SLOT(slotSteerAngle()));
        //        connect(btnZero,SIGNAL(clicked(bool)),pControlTimer,SLOT(slotZeroAngle()));
        btnZero->setToolTip(tr("Set the steering wheel angle"));
        // ui->gridLayout_button->addWidget(btnW,0,1);
        // ui->gridLayout_button->addWidget(btnS,2,1);
        // ui->gridLayout_button->addWidget(btnA,1,0);
        // ui->gridLayout_button->addWidget(btnD,1,2);
        // ui->gridLayout_button->addWidget(btnQ,1,1);
        // ui->gridLayout_button->addWidget(btnZero,2,0);
        // ui->gridLayout_button->addWidget(btnSetting,2,2);
        //if(ui->gamToolBtn)
            // ui->gridLayout_button->addWidget(gameHandleBtn,0,2);
        ui->horizontalLayout_steer->addWidget(btnZero);
        pSteerMeter = new SteerMeter(this);
        pSteerMeter->setFixedSize(60,60);
        ui->horizontalLayout_steer->addWidget(pSteerMeter);
        pSteerMeter->setForeground(Qt::black);
        QStringList list;
        for(int i=0;i<181;++i){
            if(0<90){
                list.append(QString(QStringLiteral("%1°")).arg(90-i));
            }else if(i == 90){
                list.append(QString(QStringLiteral("%1°")).arg(0));
            }else{
                list.append(QString(QStringLiteral("%1°")).arg(i-90));
            }
        }
        pSteerMeter->setCustomTextList(list);
    }
    break;
    case 2:
    case 3:
    {
        ui->btnE->show();
        ui->btnR->show();
        _shortcutKeys<<"W"<<"S"<<"A"<<"D"<<"E"<<"R"<<"Q";
        // ui->gridLayout_button->addWidget(btnE,1,1);
        // ui->gridLayout_button->addWidget(btnR,1,3);
        // ui->gridLayout_button->addWidget(btnSetting,2,0);
        // ui->gridLayout_button->addWidget(btnQ,1,2);
        // ui->gridLayout_button->addWidget(btnW,0,2);
        // ui->gridLayout_button->addWidget(btnS,2,2);
        // ui->gridLayout_button->addWidget(btnA,1,0);
        // ui->gridLayout_button->addWidget(btnD,1,4);
        // if(gameHandleBtn)
        //     ui->gridLayout_button->addWidget(gameHandleBtn,1,0);
    }
    break;
    case 5:
    {
        ui->btnE->show();
        ui->btnR->show();
        _shortcutKeys<<"W"<<"S"<<"A"<<"D"<<"E"<<"R"<<"Q";
        // ui->gridLayout_button->addWidget(btnE,1,1);
        // ui->gridLayout_button->addWidget(btnR,1,3);
        // ui->gridLayout_button->addWidget(btnSetting,2,0);
        // ui->gridLayout_button->addWidget(btnQ,1,2);
        // ui->gridLayout_button->addWidget(btnW,0,2);
        // ui->gridLayout_button->addWidget(btnS,2,2);
        // ui->gridLayout_button->addWidget(btnA,1,0);
        // ui->gridLayout_button->addWidget(btnD,1,4);
        //if(gameHandleBtn)
            // ui->gridLayout_button->addWidget(gameHandleBtn,0,3);
        _steerMap.clear();
        _motorSteerMap.clear();
        for(int i=0; i<_steerList.size(); ++i){
            auto map = _steerList.at(i).toMap();
            auto steerMeter = new SteerMeter(this);
            steerMeter->setFixedSize(60,60);
            ui->horizontalLayout_steer->addWidget(steerMeter);
            auto motorName = map.value("motorName").toString();
            steerMeter->setForeground(Qt::black);
            steerMeter->setToolTip(motorName);
            QStringList list;
            for(int i=0;i<181;++i){
                if(0<90){
                    list.append(QString(QStringLiteral("%1°")).arg(90-i));
                }else if(i == 90){
                    list.append(QString(QStringLiteral("%1°")).arg(0));
                }else{
                    list.append(QString(QStringLiteral("%1°")).arg(i-90));
                }
            }
            steerMeter->setCustomTextList(list);
            _steerMap.insert(QString("steer_angles%1").arg(i+1),steerMeter);
            _motorSteerMap.insert(motorName,steerMeter);
        }
    }
    break;
    default:
    {
           _shortcutKeys<<"W"<<"S"<<"A"<<"D"<<"Q";
        // ui->gridLayout_button->addWidget(btnW,0,1);
        // ui->gridLayout_button->addWidget(btnS,2,1);
        // ui->gridLayout_button->addWidget(btnA,1,0);
        // ui->gridLayout_button->addWidget(btnD,1,2);
        // ui->gridLayout_button->addWidget(btnQ,1,1);
        // ui->gridLayout_button->addWidget(btnSetting,2,2);
        //if(gameHandleBtn)
            // ui->gridLayout_button->addWidget(gameHandleBtn,0,2);
    }
    break;
    }
    //非机器人隐藏这些按钮
    //_peripheralBtn->setVisible(RobotInterface::E_Cell_Robot == PublicClass::init()->_iCellType? true : false);
}
void ControlRobotMoveWidget::slotSendGlobalMsg(int sgs, const QVariant &var)
{
    if(sgs == SendGlobalMsg::E_GS_keyPress){
        // if(!this->isVisible()) return;//扫图界面不再显示控制器界面.
        QString key = var.toString();
        if(!_shortcutKeys.contains(key)){
            return;
        }
        if(pControlTimer){
            pControlTimer->setControlType(ControlTimer::E_Keyboard);
        }
        pressedMoveKey(key);
    }else if(sgs == SendGlobalMsg::E_GS_keyRelease){
        QString key = var.toString();
        if(key != "AQ"){
            if(!_shortcutKeys.contains(key)){
                return;
            }
        }
        releaseMoveKey(key,"SendGlobalMsg::E_GS_keyRelease");
    }
}

void ControlRobotMoveWidget::keyPressEvent(QKeyEvent *e)
{
    QWidget::keyPressEvent(e);
}
void ControlRobotMoveWidget::keyReleaseEvent(QKeyEvent *e)
{
    QWidget::keyReleaseEvent(e);
}

void ControlRobotMoveWidget::leaveEvent(QEvent *e)
{
    QWidget::leaveEvent(e);
}

int ControlRobotMoveWidget::chassisMode() const
{
    return _mode;
}

QStringList ControlRobotMoveWidget::getShortcutKeys() const
{
    return _shortcutKeys;
}

void ControlRobotMoveWidget::setShortcutKeys(const QStringList &value)
{
    _shortcutKeys = value;
}

bool ControlRobotMoveWidget::pressedMoveKey(const QString& key)
{
    auto tcpManager = RobotManager::init()->getCurrentRobot();
    if(!tcpManager || !tcpManager->isConnected()){
        SCDebug<<tr("The robot is not connected and cannot be controlled");
        return false;
    }
    if(pControlTimer){
        if(_mode == 4 && key == "E"){
            pControlTimer->slotZeroAngle();
            return true;
        }
        if(pControlTimer->buttonMap().keys().contains(key)){
            pControlTimer->startMove(key,true);
        }
        if(pControlTimer->buttonArea().keys().contains(key)){
            if(key == "Q"){
                //紧急停止
                pControlTimer->emergencyStop();
                emit pControlTimer->sigKeyChecked(key);
            }else{
                pControlTimer->startMove(key,true);
            }
        }
    }
    return true;
}

bool ControlRobotMoveWidget::releaseMoveKey(const QString &key,const QString &fromClassName)
{
    if(key != "AQ"){
        if(!_shortcutKeys.contains(key)){
            return false;
        }
    }
    if(_cMaskWidget && _cMaskWidget->isVisible())
        _cMaskWidget->setVisible(false);
    //    SCDebug<<key;
    auto tcpManager = RobotManager::init()->getCurrentRobot();
    if(!tcpManager || !tcpManager->isConnected()){
        SCDebug<<tr("The robot is not connected and cannot be controlled");
        return false;
    }
    if(pControlTimer)
        pControlTimer->releaseKey(key,fromClassName);
    if(pControlTimer && key == "Q"){
        emit pControlTimer->sigKeyUnChecked("Q");
    }
    return true;
}

void ControlRobotMoveWidget::setSteerValue(const QString &key,qreal value)
{
    auto steerMeter = _steerMap.value(key);
    if(!steerMeter) return;
    int iValue = qRadiansToDegrees(value);
    if(iValue ==0 ){
        iValue = 90;
    }else if(iValue>0 && iValue<=90){
        iValue =90-iValue;
    }else if(iValue>90){
        iValue = 0;
    }else if(iValue<0 && iValue>=-90){
        iValue = 90+abs(iValue);
    }else if(iValue<-90){
        iValue = 180;
    }else{
        iValue = 90;
    }
    QString valueText = QString("%1%2").arg(QString::number(qRadiansToDegrees(value),'f',2),QStringLiteral("°"));
    steerMeter->setValueText(valueText);
    steerMeter->setValue(iValue);
}
void ControlRobotMoveWidget::setSteerValue(const QVariantMap &map)
{
    auto motorName = map.value("motor_name").toString();
    auto steerMeter = _motorSteerMap.value(motorName);
    if(!steerMeter) return;
    qreal value = map.value("speed").toDouble();
    int iValue = qRadiansToDegrees(value);
    if(iValue ==0 ){
        iValue = 90;
    }else if(iValue>0 && iValue<=90){
        iValue =90-iValue;
    }else if(iValue>90){
        iValue = 0;
    }else if(iValue<0 && iValue>=-90){
        iValue = 90+abs(iValue);
    }else if(iValue<-90){
        iValue = 180;
    }else{
        iValue = 90;
    }
    QString valueText = QString("%1%2").arg(QString::number(qRadiansToDegrees(value),'f',2),QStringLiteral("°"));
    steerMeter->setValueText(valueText);
    steerMeter->setValue(iValue);
}

void ControlRobotMoveWidget::setValue(qreal value)
{
    if(4 == _mode){
        if(pSteerMeter){
            int iValue = qRadiansToDegrees(value);
            if(iValue==0){
                iValue = 90;
            }else if(iValue>0 && iValue<=90){
                iValue =90-iValue;
            }else if(iValue>90){
                iValue = 0;
            }else if(iValue<0 && iValue>=-90){
                iValue = 90+abs(iValue);
            }else if(iValue<-90){
                iValue = 180;
            }else{
                iValue = 90;
            }
            QString valueText =QString(QStringLiteral("%1°")).arg(QString::number(qRadiansToDegrees(value),'f',2));
            pSteerMeter->setValueText(valueText);
            pSteerMeter->setValue(iValue);
        }
    }
}

void ControlRobotMoveWidget::slotRobotControlSettingButton()
{
    if(pSettingRobotSpeed){
        delete pSettingRobotSpeed;
    }
    pSettingRobotSpeed = new SettingRobotSpeed(this);
    connect(pSettingRobotSpeed,SIGNAL(sigClosedWidget()),this,SLOT(slotSettingRobotSpeed()));
    connect(pSettingRobotSpeed,SIGNAL(sigSettingRobotSpeedParameter(qreal,qreal,qreal,qreal)),
            pControlTimer,SLOT(slotControlParameter(qreal,qreal,qreal,qreal)));
    pSettingRobotSpeed->setMotorList(_motorList);
    pSettingRobotSpeed->setRobotMaxSpeed(_robotMaxSpeed);
    pSettingRobotSpeed->setRobotMaxRot(_robotMaxRot);
    pSettingRobotSpeed->setMaxSpeed(_robotMaxSpeed);
    pSettingRobotSpeed->setMaxRot(_robotMaxRot);

    pSettingRobotSpeed->setParameterValue(
        pControlTimer->maxSpeed(),
        0,
        pControlTimer->maxRot(),
        0);
    pSettingRobotSpeed->show();
}

void ControlRobotMoveWidget::slotSettingRobotSpeed()
{
    delete pSettingRobotSpeed;
    pSettingRobotSpeed = Q_NULLPTR;
}

//0差动，全动，1差动、全动停止， 2舵轮 ，3舵轮停止
void ControlRobotMoveWidget::slotControlSpeed(int type,qreal vx,qreal vy,qreal w,qreal steer)
{
    if(fabs(vx)<0.000001 && fabs(vy)<0.000001&& fabs(w)<0.000001&& fabs(steer)<0.000001){
        //        UiClass::init()->releaseWaittingWidget(UiClass::init()->getMainWindow());
        _cMaskWidget->setVisible(false);
    }else{
        _cMaskWidget->updateWidget();
        _cMaskWidget->setVisible(true);
        //        UiClass::init()->showWaittingWidget((""),UiClass::init()->getMainWindow());
    }
    auto tcpManager = RobotManager::init()->getCurrentRobot();
    if(!tcpManager || !tcpManager->isConnected()){
        SCWarning<<tr("The robot is not connected and cannot be controlled");
        return;
    }
    QVariantMap requestMap;
    switch (type) {
    case 0://0差动、全动
    case 1:
    {
        requestMap.insert("vx",vx);
        requestMap.insert("vy",vy);
        if(tcpManager->deviceStateInfo()->product == M_Robokit4){
            requestMap.insert("vw",w);
        }else{
            requestMap.insert("w",w);
        }
        tcpManager->tracking(requestMap,RobotInterface::E_CN_ControlInterface);
    }
    break;
    case 2://2舵轮
    {
        if(tcpManager->deviceStateInfo()->product == M_Robokit4){
            requestMap.insert("vx",vx);
            requestMap.insert("steer",steer);
        }else{
            //兼容旧接口
            if(isCompatibilityMode()){
                requestMap.insert("vx",vx);
                requestMap.insert("steer",steer);//-1,0,1,(-2->0°)
            }else{
                //新接口
                requestMap.insert("vx",vx);
                requestMap.insert("vy",vy);
                requestMap.insert("w",w);
            }
        }
        tcpManager->tracking(requestMap,RobotInterface::E_CN_ControlInterface);
    }
    break;
    case 3://3急停
    {
        tcpManager->stopTracking(RobotInterface::E_CN_ControlInterface);
    }
    break;
    default:
        break;
    }
}

void ControlRobotMoveWidget::slotPeripheralBtnCliecked(bool checked)
{

    if(!_periperalListDlg){
        _periperalListDlg = new PeripheralListDlg(this);
        connect(_periperalListDlg,&PeripheralListDlg::sigClosedWidget,this,[this](){
            delete _periperalListDlg;
            _periperalListDlg = Q_NULLPTR;
        });
    }
    _periperalListDlg->show();
    _periperalListDlg->raise();
}

void ControlRobotMoveWidget::slotSteerAngle()
{
    if(!_steerAngleDlg){
        _steerAngleDlg = new SteerAngleDlg(this);
        connect(_steerAngleDlg,SIGNAL(sigSteerAngleDlgParameter(qreal)),this,SLOT(slotSteerAngleDlgParameter(qreal)));
        connect(_steerAngleDlg,SIGNAL(sigClosedWidget()),this,SLOT(slotSteerAngleDlgClosed()));
    }
    _steerAngleDlg->show();
    _steerAngleDlg->raise();
}
void ControlRobotMoveWidget::updateRobotStateInfo(const RO_Network::RobotStateInfo*info)
{
    if(pSettingRobotSpeed){
        pSettingRobotSpeed->updateRobotStateInfo(info);
    }
}

void ControlRobotMoveWidget::slotSteerAngleDlgClosed()
{
    if(pControlTimer)
        pControlTimer->setCompatibilityMode(isCompatibilityMode());
}

void ControlRobotMoveWidget::slotSteerAngleDlgParameter(qreal angle)
{
    QVariantMap json_map;
    //新旧接口都支持角度
    //    if(!isCompatibilityMode()){
    //        //舵轮新接口
    json_map.insert("real_steer",qDegreesToRadians(angle));
    //    }else{
    //        //        if(angle <0.000001 && angle >-0.000001){
    //        //舵轮旧接口
    //        json_map.insert("steer",-2);//-1,0,1,(-2->0°)
    //        //        }else{
    //        //            json_map.insert("real_steer",qDegreesToRadians(angle)); //角度->弧度
    //        //        }
    //    }
    RobotManager::init()->sendData( RobotInterface::E_ControlTcp,QVariant(),json_map,"robot_control_motion_req",_ip,RobotInterface::E_CN_ControlInterface);
}

bool ControlRobotMoveWidget::event(QEvent *event)
{
    switch (event->type()) {
    case QEvent::Show:
        for (auto &var : _shortcutList) {
            var->setEnabled(true);
        }
        break;
    case QEvent::Hide:
        for (auto &var : _shortcutList) {
            var->setEnabled(false);
        }
        break;
    default:
        break;
    }
    return QWidget::event(event);
}

QString ControlRobotMoveWidget::getIp() const
{
    return _ip;
}

void ControlRobotMoveWidget::setIp(const QString &ip)
{
    _ip = ip;
}

void ControlRobotMoveWidget::stopAllTimer()
{
    if(pControlTimer){
        pControlTimer->stopAllTimer();
        pControlTimer->clearAllBtnState();
        pControlTimer->clearAllBtnAreaState();
    }
}

bool ControlRobotMoveWidget::isCompatibilityMode() const
{
    return   (_steerAngleDlg && _steerAngleDlg->isCompatibilityMode()) ? true : false;
}
}//namespace
